﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    public class UserRegistryService
    {
        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(UserRegistry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into UserRegistry(");
            strSql.Append("Code,UserName,PassWord,ConfirmPassWord,Email,IsEmailVerification,Remark,State,CreateID,CreateTime,Sort)");
            strSql.Append(" values (");
            strSql.Append("@Code,@UserName,@PassWord,@ConfirmPassWord,@Email,@IsEmailVerification,@Remark,@State,@CreateID,@CreateTime,@Sort)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
                    new SqlParameter("@UserName", SqlDbType.NVarChar,200),
                    new SqlParameter("@PassWord", SqlDbType.NVarChar,200),
                    new SqlParameter("@ConfirmPassWord", SqlDbType.NVarChar,200),
					new SqlParameter("@Email", SqlDbType.NVarChar,200),
					new SqlParameter("@IsEmailVerification", SqlDbType.Bit,1),					
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
                    new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.PassWord;
            parameters[3].Value = model.ConfirmPassWord;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.IsEmailVerification;            
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.State;
            parameters[8].Value = model.CreateID;
            parameters[9].Value = model.CreateTime;
            parameters[10].Value = model.Sort;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UserRegistry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserRegistry set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("PassWord=@PassWord,");
            strSql.Append("ConfirmPassWord=@ConfirmPassWord,");
            strSql.Append("Email=@Email,");
            strSql.Append("IsEmailVerification=@IsEmailVerification,");            
            strSql.Append("Remark=@Remark,");          
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
						new SqlParameter("@Code", SqlDbType.VarChar,36),
                      new SqlParameter("@UserName", SqlDbType.NVarChar,200),
                    new SqlParameter("@PassWord", SqlDbType.NVarChar,200),
                    new SqlParameter("@ConfirmPassWord", SqlDbType.NVarChar,200),
					new SqlParameter("@Email", SqlDbType.NVarChar,200),
					new SqlParameter("@IsEmailVerification", SqlDbType.Bit,1),					
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
                    new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.PassWord;
            parameters[3].Value = model.ConfirmPassWord;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.IsEmailVerification;            
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.State;
            parameters[8].Value = model.CreateID;
            parameters[9].Value = model.CreateTime;
            parameters[10].Value = model.Sort;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserRegistry ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserRegistry GetModel(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Code, UserName,PassWord,ConfirmPassWord,Email,IsEmailVerification,Remark,State,CreateID,CreateTime,Sort from UserRegistry ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            Advertisement model = new Advertisement();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<UserRegistry> GetAll(int pageIndex, int pageSize, out int RecordCount)
        {
            string strsql = @"select Code,UserName,PassWord,ConfirmPassWord,Email,IsEmailVerification,Remark,State,CreateID,CreateTime,Sort from UserRegistry order by sort desc";
            List<UserRegistry> list = new List<UserRegistry>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<UserRegistry> GetAll()
        {
            string strsql = @"select Code,UserName,PassWord,ConfirmPassWord,Email,IsEmailVerification,Remark,State,CreateID,CreateTime,Sort from UserRegistry order by sort desc";
            List<UserRegistry> list = new List<UserRegistry>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserRegistry DataRowToModel(SqlDataReader row)
        {
            UserRegistry model = new UserRegistry();
            if (row != null)
            {
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["UserName"] != null)
                {
                    model.UserName =DecryptService.Decrypt(row["UserName"].ToString());
                }
                if (row["PassWord"] != null)
                {
                    model.PassWord = DecryptService.Decrypt(row["PassWord"].ToString());
                }
                if (row["ConfirmPassWord"] != null)
                {
                    model.ConfirmPassWord = DecryptService.Decrypt(row["ConfirmPassWord"].ToString());
                }

                if (row["Email"] != null)
                {
                    model.Email = DecryptService.Decrypt(row["Email"].ToString());
                }
                if (row["IsEmailVerification"] != null)
                {
                    model.IsEmailVerification = (bool)row["IsEmailVerification"];
                }
             
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["CreateID"] != null)
                {
                    model.CreateID = row["CreateID"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
            }
            return model;
        }

    }
}
